package com.ui;

import com.service.ClassService;
import com.service.GroupService;
import com.service.StudentService;
import com.database.DatabaseConnection;
import com.model.Student;
import com.model.Group;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

public class MainUI {
    private static JLabel currentClassLabel;
    private static String currentClass = "请选择一个班级";
    private static GroupService groupService;
    private static StudentService studentService;

    public static void main(String[] args) {
        JFrame frame = new JFrame("Class Management System");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);

        JPanel mainPanel = new JPanel(new BorderLayout());
        frame.add(mainPanel);

        JPanel buttonPanel = new JPanel();
        ClassService classService = new ClassService(new DatabaseConnection());
        GroupService groupService = new GroupService(new DatabaseConnection()); // 初始化 groupService
        StudentService studentService = new StudentService(new DatabaseConnection());

        createButtonWithMenu(buttonPanel, "班级管理", "添加班级", "管理班级", () -> showAddClassDialog(classService), () -> showClassManagementUI(classService));
        createButtonWithMenu(buttonPanel, "小组管理", "添加小组", "管理小组", () -> showAddGroupDialog(groupService, currentClass), () -> showGroupManagementUI(groupService, currentClass));
        createButtonWithMenu(buttonPanel, "学生管理", "添加学生", "管理学生", () -> showAddStudentDialog(studentService,groupService, currentClass), () -> showStudentManagementUI(studentService, currentClass));
      createButtonWithMenu(buttonPanel, "课堂管理", "随机小组", "随机学生", () -> showRandomGroupDialog(groupService, currentClass), () -> showRandomStudentDialog(studentService, groupService, currentClass));

        mainPanel.add(buttonPanel, BorderLayout.NORTH);

        JPanel labelPanel = new JPanel();
        currentClassLabel = new JLabel(currentClass);
        labelPanel.add(currentClassLabel);

        mainPanel.add(labelPanel, BorderLayout.SOUTH);

        frame.setVisible(true);
    }

    private static void createButtonWithMenu(JPanel panel, String buttonLabel, String menu1, String menu2, Runnable action1, Runnable action2) {
        JButton button = new JButton(buttonLabel);
        panel.add(button);

        JPopupMenu menu = new JPopupMenu();
        JMenuItem menuItem1 = new JMenuItem(menu1);
        JMenuItem menuItem2 = new JMenuItem(menu2);

        menu.add(menuItem1);
        menu.add(menuItem2);

        button.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                menu.show(button, 0, button.getHeight());
            }
        });

        menuItem1.addActionListener(e -> action1.run());
        menuItem2.addActionListener(e -> action2.run());
    }
    private static void showAddClassDialog(ClassService classService) {
        AddClassDialog addClassDialog = new AddClassDialog(classService);
        addClassDialog.setVisible(true);
    }
 
private static void showAddStudentDialog(StudentService studentService, GroupService groupService, String currentClass) {
    if (currentClass == null || currentClass.isEmpty()) {
        JOptionPane.showMessageDialog(null, "请先选择一个班级", "错误", JOptionPane.ERROR_MESSAGE);
        return;
    }

    // 使用班级名称获取小组列表
    List<Group> groups = groupService.getGroupListByClassName(currentClass);
    System.out.println("获取的小组列表: " + groups); // 添加调试信息

    if (groups == null || groups.isEmpty()) {
        JOptionPane.showMessageDialog(null, "当前班级下没有小组", "错误", JOptionPane.ERROR_MESSAGE);
        return;
    }

    // 创建并显示 AddStudentDialog 对话框，并传递小组列表
    AddStudentDialog addStudentDialog = new AddStudentDialog(studentService, groupService, currentClass, groups);
    addStudentDialog.setVisible(true);
}

    private static void showClassManagementUI(ClassService classService) {
        ClassManagementUI classManagementUI = new ClassManagementUI(classService, className -> {
            currentClass = className;
            currentClassLabel.setText("当前班级：" + className);
        });
        classManagementUI.setVisible(true);
    }
    private static void showAddGroupDialog(GroupService groupService, String currentClass) {
        if (currentClass == null || currentClass.isEmpty()) {
            JOptionPane.showMessageDialog(null, "请先选择一个班级", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }
        AddGroupDialog addGroupDialog = new AddGroupDialog(groupService, currentClass);
        addGroupDialog.setVisible(true);
    }
    private static void showGroupManagementUI(GroupService groupService, String currentClass) {
        GroupManagementUI groupManagementUI = new GroupManagementUI(groupService, currentClass);
        groupManagementUI.setVisible(true);
    }
    private static void showStudentManagementUI(StudentService studentService, String currentClass) {
        List<Student> students = studentService.getAllStudents(currentClass);
        StudentManagementUI studentManagementUI = new StudentManagementUI(studentService, currentClass, students);
        studentManagementUI.setVisible(true);
    }

    // 显示随机小组对话框
    private static void showRandomGroupDialog(GroupService groupService, String currentClass) {
        if (currentClass == null || currentClass.isEmpty()) {
            JOptionPane.showMessageDialog(null, "请先选择一个班级", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        // 使用班级名称获取小组列表
        List<Group> groups = groupService.getGroupListByClassName(currentClass);

        
        if (groups == null || groups.isEmpty()) {
            JOptionPane.showMessageDialog(null, "当前班级下没有小组", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }

        RandomGroupDialog randomGroupDialog = new RandomGroupDialog(studentService, groupService, currentClass, groups);
        randomGroupDialog.setVisible(true);
    }

    // 显示随机学生对话框
    private static void showRandomStudentDialog(StudentService studentService, GroupService groupService, String currentClass) {
        if (currentClass == null || currentClass.isEmpty()) {
            JOptionPane.showMessageDialog(null, "请先选择一个班级", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }
        RandomStudentDialog randomStudentDialog = new RandomStudentDialog(studentService, groupService, currentClass);
        randomStudentDialog.setVisible(true);
    }
}